package com.lyeducation.social.wechatmp.config;

import com.lyeducation.security.config.SecurityConstants;
import com.lyeducation.social.wechatmp.connect.WeChatMPConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.autoconfigure.SocialAutoConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;

import javax.sql.DataSource;

// import cn.merryyou.logback.social.SocialAutoConfigurerAdapter;

/**
 * Created on 2018/1/11.
 *
 * @author zlf
 * @since 1.0
 */
@Configuration
public class WeChatMPAuthConfig extends SocialAutoConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private ConnectionSignUp myConnectionSignUp;

    @Override
    protected ConnectionFactory<?> createConnectionFactory() {
        return new WeChatMPConnectionFactory(
                SecurityConstants.DEFAULT_SOCIAL_WECHATMP_PROVIDER_ID,
//                SecurityConstants.DEFAULT_SOCIAL_WEIXIN_APP_ID,
//                SecurityConstants.DEFAULT_SOCIAL_WEIXIN_APP_SECRET
                SecurityConstants.DEFAULT_SOCIAL_WECHATMP_APP_ID,
                SecurityConstants.DEFAULT_SOCIAL_WECHATMP_APP_SECRET
        );
    }

    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource,
                connectionFactoryLocator, Encryptors.noOpText());
        if (myConnectionSignUp != null) {
            repository.setConnectionSignUp(myConnectionSignUp);
        }
        return repository;
    }

//    /**
//     * /connect/weixin POST请求,绑定微信返回connect/weixinConnected视图 /connect/weixin
//     * DELETE请求,解绑返回connect/weixinConnect视图
//     *
//     * @return
//     */
//    @Bean({"connect/weixinConnect", "connect/weixinConnected"})
//    @ConditionalOnMissingBean(name = "weixinConnectedView")
//    public View weixinConnectedView() {
//        return new SocialConnectView();
//    }
}
